package cn.hhchat.wp.config;

import cn.hhchat.wp.config.properties.EmailProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * Created this one by HMH on 2017/6/29.
 */
@Configuration
public class MailConfig {

    private EmailProperties emailConfig;

    @Autowired
    public MailConfig(SystemProperties sysConfig) {
        this.emailConfig = sysConfig.getEmail();
    }

    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        Properties emailProperties = new Properties();

        emailProperties.put("mail.smtp.host", emailConfig.getHost());
        emailProperties.put("mail.transport.protocol", "smtp");
        emailProperties.put("mail.smtp.user", emailConfig.getUsername());
        emailProperties.put("mail.smtp.port", emailConfig.getPort());
        emailProperties.put("mail.smtp.auth", "true");
        emailProperties.put("mail.smtp.socketFactory.port", emailConfig.getSocketFactoryPort());
        emailProperties.put("mail.smtp.socketFactory.class", emailConfig.getSocketFactoryClass());
        javaMailSender.setPassword(emailConfig.getPwd());
        javaMailSender.setJavaMailProperties(emailProperties);
        javaMailSender.setDefaultEncoding("UTF-8");
        return javaMailSender;
    }
}
